package ustc.running.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import ustc.running.R;
import ustc.running.data.SoftwareData;
import ustc.running.util.MyHandler;
import ustc.running.util.VibratorUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 关于软件
 * @author 李跃东
 * @mail androiddevelop@qq.com
 * @date 2013-11-7
 */
@SuppressWarnings("deprecation")
public class AboutSoftwareUi extends Activity {
	private TextView softName,softVersion,softContent;
	private Button exitButton;
	private MyHandler handler;   
	private String address;
	private Resources resources;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		resources=AboutSoftwareUi.this.getResources();
		this.setTitle(resources.getString(R.string.about_software));
		this.setContentView(R.layout.about_software_ui);

		softName=(TextView)findViewById(R.id.softwareName);
		softVersion=(TextView)findViewById(R.id.softwareVersion);
		softContent=(TextView)findViewById(R.id.softwareContent);
		exitButton=(Button)findViewById(R.id.closeButton);

		softName.setText(this.getResources().getString(R.string.app_name));
		softVersion.setText(this.getResources().getString(R.string.app_version)+SoftwareData.VERSION);
		softContent.setText(this.getResources().getString(R.string.app_introduct));
		softContent.setOnLongClickListener(new TextView.OnLongClickListener() {
			public boolean onLongClick(View v) {
				VibratorUtil.vibrateOnce(AboutSoftwareUi.this);
				//当前最小sdk为android2.2，必须采用老版本剪切板api
				ClipboardManager clipboardManager=(ClipboardManager)AboutSoftwareUi.this.getSystemService(Service.CLIPBOARD_SERVICE);
				clipboardManager.setText(SoftwareData.MAIL);
				Toast.makeText(AboutSoftwareUi.this, AboutSoftwareUi.this.getResources().getString(R.string.copy_success),Toast.LENGTH_SHORT).show();
				return true;
			}
		});

		exitButton.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				AboutSoftwareUi.this.finish();				
			}
		});

		handler=new MyHandler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				new AlertDialog.Builder(AboutSoftwareUi.this)
				.setMessage(resources.getString(R.string.find_new_version))
				.setPositiveButton(resources.getString(R.string.update),
						new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent intent=new Intent();
						intent.setData(Uri.parse(address));
						intent.setAction(Intent.ACTION_VIEW);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						AboutSoftwareUi.this.startActivity(intent);
					}
				}).setNegativeButton(resources.getString(R.string.cancel),
						new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				}).show();

			}

		};

		new Thread(){
			public void run(){
				URL url;
				try {
					url = new URL(SoftwareData.UPDATE_ADDRESS);
					URLConnection connection = url.openConnection();  
					connection.setReadTimeout(60000);
					BufferedReader buff=new BufferedReader(new InputStreamReader(connection.getInputStream()));
					double tmp=Double.valueOf(buff.readLine());
					address=buff.readLine();		
					if(tmp>SoftwareData.VERSION){
						handler.sendEmptyMessage(0);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
}
